package com.vincce.fitness.api;
import com.vincce.fitness.core.util.WechatUtil;
import com.vincce.fitness.entity.WechatMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Created by henry on 5/4/14.
 */
@Controller
@RequestMapping("/api")
public class WechatController {

	@Resource
	WechatService wechatService;

	@RequestMapping(value = "wechat", method = RequestMethod.GET)
	@ResponseBody
	public String wechat(String signature, String timestamp, String nonce, @RequestParam("echostr") String echoStr) {
		return WechatUtil.checkSignature(signature, timestamp, nonce, echoStr) ? echoStr : "";
	}

	@RequestMapping(value = "wechat", method = RequestMethod.POST)
	@ResponseBody
	public String wechat(@RequestBody String body) throws IOException {
		System.out.println("[body wechat]" + body);
		WechatMessage returnMessage = wechatService.process(body);
		return returnMessage == null ? "" : WechatUtil.messageToXml(returnMessage);
	}

}
